import wx
import sqlite3


# Layout Resources Class
class LayoutContentTab(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        
        self.toolbar_ADDMODULE = wx.NewId()
        self.toolbar_DELMODULE = wx.NewId()
        self.toolbar_ADDPAGE = wx.NewId()
        self.toolbar_DELPAGE = wx.NewId()
        self.toolbar_MODULEDOWN = wx.NewId()
        self.toolbar_MODULEUP = wx.NewId()
        self.toolbar_PAGEDOWN = wx.NewId()
        self.toolbar_PAGEUP = wx.NewId()
        self.toolbar_ADDRESOURCE = wx.NewId()
        self.toolbar_DELRESOURCE = wx.NewId()
        self.toolbar_RESOURCEUP = wx.NewId()
        self.toolbar_RESOURCEDOWN = wx.NewId()

        self.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_3DFACE))
        self.mainBox = wx.BoxSizer(wx.HORIZONTAL)
        self.leftBox = wx.BoxSizer(wx.VERTICAL)
        self.centerBox = wx.BoxSizer(wx.VERTICAL)
        self.rightBox = wx.BoxSizer(wx.VERTICAL)
        self.leftStaticBox = wx.StaticBox(self, -1, "Modules")
        self.leftStaticBoxSizer = wx.StaticBoxSizer(self.leftStaticBox)
        self.centerStaticBox = wx.StaticBox(self, -1, "Pages")
        self.centerStaticBoxSizer = wx.StaticBoxSizer(self.centerStaticBox)
        self.rightStaticTopBox = wx.StaticBox(self, -1, "Available Resources")
        self.rightStaticTopBoxSizer = wx.StaticBoxSizer(self.rightStaticTopBox)
        self.rightStaticBotBox = wx.StaticBox(self, -1, "Resources on Selected Page")
        self.rightStaticBotBoxSizer = wx.StaticBoxSizer(self.rightStaticBotBox)
        self.moduleToolbar = self.MakeModuleToolbar()
        self.pageToolbar = self.MakePageToolbar()
        self.resourcesToolbar = self.MakeResourcesToolbar()
        self.moduleListBox = wx.ListBox(self, -1, style= wx.BORDER_SUNKEN | wx.LB_SINGLE | wx.LB_NEEDED_SB)
        self.pageListBox = wx.ListBox(self, -1, style =wx.BORDER_SUNKEN | wx.LB_SINGLE | wx.LB_NEEDED_SB)
        self.availableResourcesListBox = wx.ListBox(self, -1, style=wx.BORDER_SUNKEN | wx.LB_SINGLE | wx.LB_NEEDED_SB)
        self.usedResourcesListBox = wx.ListBox(self, -1, style=wx.BORDER_SUNKEN | wx.LB_SINGLE | wx.LB_NEEDED_SB)
        self.leftBox.Add(self.moduleToolbar, 0, wx.ALL | wx.ALIGN_LEFT | wx.EXPAND)
        self.leftBox.Add(self.moduleListBox, 1, wx.EXPAND)
        self.centerBox.Add(self.pageToolbar, 0, wx.ALL | wx.ALIGN_LEFT | wx.EXPAND)
        self.centerBox.Add(self.pageListBox, 1, wx.EXPAND)
        self.rightStaticTopBoxSizer.Add(self.availableResourcesListBox, 1, wx.EXPAND)
        self.rightStaticBotBoxSizer.Add(self.usedResourcesListBox, 1, wx.EXPAND)
        self.rightBox.Add(self.rightStaticTopBoxSizer, 1, wx.EXPAND)
        self.rightBox.Add(self.resourcesToolbar, 0, wx.ALL | wx.ALIGN_LEFT | wx.EXPAND)
        self.rightBox.Add(self.rightStaticBotBoxSizer, 1, wx.EXPAND)
        self.leftStaticBoxSizer.Add(self.leftBox, 0, wx.ALL | wx.ALIGN_TOP | wx.EXPAND)
        self.centerStaticBoxSizer.Add(self.centerBox, 0, wx.ALL | wx.ALIGN_TOP | wx.EXPAND)
        self.mainBox.Add(self.leftStaticBoxSizer, 0, wx.ALL | wx.ALIGN_TOP | wx.EXPAND)
        self.mainBox.Add(self.centerStaticBoxSizer, 0, wx.ALL | wx.ALIGN_TOP | wx.EXPAND)
        self.mainBox.Add(self.rightBox, 1, wx.EXPAND)
        self.SetSizer(self.mainBox)
        
        wx.EVT_LISTBOX(self, self.moduleListBox.GetId(), self.OnSelectedModuleItem) # may not be event associated with selecting them...
        wx.EVT_LISTBOX(self, self.pageListBox.GetId(), self.OnSelectedPageItem)
        wx.EVT_LISTBOX(self, self.usedResourcesListBox.GetId(), self.OnSelectedUsedResourceItem)
        wx.EVT_LISTBOX(self, self.availableResourcesListBox.GetId(), self.OnSelectedAvailableResourceItem)
        
        
    def OnSelectedModuleItem(self,evt):
        if (self.moduleListBox.GetSelection() != -1):
            self.pageToolbar.EnableTool(self.toolbar_ADDPAGE, True)
            self.moduleToolbar.EnableTool(self.toolbar_DELMODULE, True)
            if(self.moduleListBox.GetSelection() == 0):
                self.moduleToolbar.EnableTool(self.toolbar_MODULEDOWN, True)
                self.moduleToolbar.EnableTool(self.toolbar_MODULEUP, False)
            elif(self.moduleListBox.GetSelection() == (self.moduleListBox.GetCount() - 1)):
                self.moduleToolbar.EnableTool(self.toolbar_MODULEDOWN, False)
                self.moduleToolbar.EnableTool(self.toolbar_MODULEUP, True)
            else:
                self.moduleToolbar.EnableTool(self.toolbar_MODULEDOWN, True)
                self.moduleToolbar.EnableTool(self.toolbar_MODULEUP, True)
                
            self.resourcesToolbar.EnableTool(self.toolbar_DELRESOURCE, False)
            self.resourcesToolbar.EnableTool(self.toolbar_ADDRESOURCE, False)
            self.resourcesToolbar.EnableTool(self.toolbar_RESOURCEUP, False)
            self.resourcesToolbar.EnableTool(self.toolbar_RESOURCEDOWN, False)
            self.TopLevelParent.RefreshPageListBox(self.pageListBox, self.TopLevelParent.currentCourseID, self.TopLevelParent.GetStructureID(self.moduleListBox.GetSelection(), 1, self.TopLevelParent.currentCourseID))
        
    def OnSelectedPageItem(self, evt):
        if (self.pageListBox.GetSelection() != -1):
            self.pageToolbar.EnableTool(self.toolbar_DELPAGE, True)
            if(self.pageListBox.GetSelection() == 0):
                self.pageToolbar.EnableTool(self.toolbar_PAGEDOWN, True)
                self.pageToolbar.EnableTool(self.toolbar_PAGEUP, False)
            elif(self.pageListBox.GetSelection() == (self.pageListBox.GetCount() - 1)):
                self.pageToolbar.EnableTool(self.toolbar_PAGEDOWN, False)
                self.pageToolbar.EnableTool(self.toolbar_PAGEUP, True)
            else:
                self.pageToolbar.EnableTool(self.toolbar_PAGEDOWN, True)
                self.pageToolbar.EnableTool(self.toolbar_PAGEUP, True)

            self.TopLevelParent.currentUsedResourceDict = self.TopLevelParent.RefreshListBox(self.usedResourcesListBox, self.TopLevelParent.currentCourseID, -1, self.TopLevelParent.GetStructureID(self.pageListBox.GetSelection(), 2, self.TopLevelParent.currentCourseID))
            self.TopLevelParent.DisplayDebug("put refresh call here for resources on page")
    
    def OnSelectedUsedResourceItem(self, evt):
        if(self.usedResourcesListBox.GetSelection() != -1):
            self.resourcesToolbar.EnableTool(self.toolbar_DELRESOURCE, True)
            self.resourcesToolbar.EnableTool(self.toolbar_ADDRESOURCE, False)
            if(self.usedResourcesListBox.GetSelection() == 0):
                self.resourcesToolbar.EnableTool(self.toolbar_RESOURCEUP, False)
                self.resourcesToolbar.EnableTool(self.toolbar_RESOURCEDOWN, True)
            elif(self.usedResourcesListBox.GetSelection() == (self.usedResourcesListBox.GetCount() - 1)):
                self.resourcesToolbar.EnableTool(self.toolbar_RESOURCEDOWN, False)
                self.resourcesToolbar.EnableTool(self.toolbar_RESOURCEUP, True)
            else:
                self.resourcesToolbar.EnableTool(self.toolbar_RESOURCEUP, True)
                self.resourcesToolbar.EnableTool(self.toolbar_RESOURCEDOWN, True)
    
    def OnSelectedAvailableResourceItem(self, evt):
        if(self.availableResourcesListBox.GetSelection() != -1):
            self.resourcesToolbar.EnableTool(self.toolbar_DELRESOURCE, False)
            self.resourcesToolbar.EnableTool(self.toolbar_ADDRESOURCE, True)
    
    # Create Module Toolbar
    def MakeModuleToolbar(self):
        tb = wx.ToolBar(self, -1, style=wx.TB_FLAT | wx.NO_BORDER)
        tb.AddTool(self.toolbar_ADDMODULE, wx.Bitmap("images/list-add.png", wx.BITMAP_TYPE_PNG), shortHelpString="Add Module")
        tb.AddTool(self.toolbar_DELMODULE, wx.Bitmap("images/list-remove.png", wx.BITMAP_TYPE_PNG), shortHelpString="Remove Selected Module")
        tb.AddSeparator()
        tb.AddTool(self.toolbar_MODULEDOWN, wx.Bitmap("images/go-down.png", wx.BITMAP_TYPE_PNG), shortHelpString="Move Selected Module Down")
        tb.AddTool(self.toolbar_MODULEUP, wx.Bitmap("images/go-up.png", wx.BITMAP_TYPE_PNG), shortHelpString="Move Selected Module Up")
        
        tb.EnableTool(self.toolbar_DELMODULE, False)
        tb.EnableTool(self.toolbar_MODULEDOWN, False)
        tb.EnableTool(self.toolbar_MODULEUP, False)
        
        wx.EVT_TOOL(self, self.toolbar_ADDMODULE, self.AddModule)
        wx.EVT_TOOL(self, self.toolbar_DELMODULE, self.DelModule)
        wx.EVT_TOOL(self, self.toolbar_MODULEDOWN, self.ModuleDown)
        wx.EVT_TOOL(self, self.toolbar_MODULEUP, self.ModuleUp)
        
        tb.Realize()
        
        return tb
    
    # Create Page Toolbar
    def MakePageToolbar(self):
        tb = wx.ToolBar(self, -1, style=wx.TB_FLAT | wx.NO_BORDER)
        tb.AddTool(self.toolbar_ADDPAGE, wx.Bitmap("images/list-add.png", wx.BITMAP_TYPE_PNG), shortHelpString="Add Page to Selected Module")
        tb.AddTool(self.toolbar_DELPAGE, wx.Bitmap("images/list-remove.png", wx.BITMAP_TYPE_PNG), shortHelpString="Delete Selected Page")
        tb.AddSeparator()
        tb.AddTool(self.toolbar_PAGEDOWN, wx.Bitmap("images/go-down.png", wx.BITMAP_TYPE_PNG), shortHelpString="Move Selected Page Down")
        tb.AddTool(self.toolbar_PAGEUP, wx.Bitmap("images/go-up.png", wx.BITMAP_TYPE_PNG), shortHelpString="Move Selected Page Up")
        
        tb.EnableTool(self.toolbar_ADDPAGE, False)
        tb.EnableTool(self.toolbar_DELPAGE, False)
        tb.EnableTool(self.toolbar_PAGEDOWN, False)
        tb.EnableTool(self.toolbar_PAGEUP, False)
        
        wx.EVT_TOOL(self, self.toolbar_ADDPAGE, self.AddPage)
        wx.EVT_TOOL(self, self.toolbar_DELPAGE, self.DelPage)
        wx.EVT_TOOL(self, self.toolbar_PAGEDOWN, self.PageDown)
        wx.EVT_TOOL(self, self.toolbar_PAGEUP, self.PageUp)
        
        tb.Realize()
        
        return tb
    
    def MakeResourcesToolbar(self):
        tb = wx.ToolBar(self, -1, style=wx.TB_FLAT | wx.NO_BORDER)
        tb.AddTool(self.toolbar_ADDRESOURCE, wx.Bitmap("images/go-bottom.png", wx.BITMAP_TYPE_PNG), shortHelpString="Add Selected Resource to Page")
        tb.AddTool(self.toolbar_DELRESOURCE, wx.Bitmap("images/go-top.png", wx.BITMAP_TYPE_PNG), shortHelpString="Remove Selected Resource from Page")
        tb.AddSeparator()
        tb.AddTool(self.toolbar_RESOURCEUP, wx.Bitmap("images/go-up.png", wx.BITMAP_TYPE_PNG), shortHelpString="Move Selected Resource Up on Page")
        tb.AddTool(self.toolbar_RESOURCEDOWN, wx.Bitmap("images/go-down.png", wx.BITMAP_TYPE_PNG), shortHelpString="Move Selected Resource Down on Page")
        
        tb.EnableTool(self.toolbar_DELRESOURCE, False)
        tb.EnableTool(self.toolbar_ADDRESOURCE, False)
        tb.EnableTool(self.toolbar_RESOURCEUP, False)
        tb.EnableTool(self.toolbar_RESOURCEDOWN, False)
        
        wx.EVT_TOOL(self, self.toolbar_DELRESOURCE, self.RemoveResourceFromPage)
        wx.EVT_TOOL(self, self.toolbar_ADDRESOURCE, self.AddResourceToPage)
        wx.EVT_TOOL(self, self.toolbar_RESOURCEDOWN, self.MoveResourceDownOnPage)
        wx.EVT_TOOL(self, self.toolbar_RESOURCEUP, self.MoveResourceUpOnPage)
        
        tb.Realize()
        
        return tb
    
    def RemoveResourceFromPage(self, evt):
        if (self.usedResourcesListBox.GetSelection() != -1):
            tmpString = self.usedResourcesListBox.GetStringSelection()
            tmpString2 = tmpString.split(".", 1)
            con = sqlite3.connect(self.TopLevelParent.database)
            cur = con.cursor()
            cur.execute("UPDATE ResourceTable set structureID = (?), resourceOrderNumber = (?) where resourceID = (?)", (None, None, int(tmpString2[0])))
            con.commit()
            con.close()
            self.TopLevelParent.currentAvailableResourceDict = self.TopLevelParent.RefreshListBox(self.availableResourcesListBox, self.TopLevelParent.currentCourseID, -1, -1)
            self.TopLevelParent.currentUsedResourceDict = self.TopLevelParent.RefreshListBox(self.usedResourcesListBox, self.TopLevelParent.currentCourseID, -1, self.TopLevelParent.GetStructureID(self.pageListBox.GetSelection(), 2, self.TopLevelParent.currentCourseID))
            
        self.TopLevelParent.DisplayDebug("remove resource from page")
        
    def AddResourceToPage(self, evt):
        if (self.availableResourcesListBox.GetSelection() != -1):
            tmpString = self.availableResourcesListBox.GetStringSelection()
            tmpString2 = tmpString.split(".", 1)
            con = sqlite3.connect(self.TopLevelParent.database)
            cur = con.cursor()
            cur.execute("UPDATE ResourceTable set structureID = (?), resourceOrderNumber = (?) where resourceID = (?)", (self.TopLevelParent.GetStructureID(self.pageListBox.GetSelection(), 2, self.TopLevelParent.currentCourseID), self.usedResourcesListBox.GetCount(), int(tmpString2[0])))
            con.commit()
            con.close()
            self.TopLevelParent.currentAvailableResourceDict = self.TopLevelParent.RefreshListBox(self.availableResourcesListBox, self.TopLevelParent.currentCourseID, -1, -1)
            self.TopLevelParent.currentUsedResourceDict = self.TopLevelParent.RefreshListBox(self.usedResourcesListBox, self.TopLevelParent.currentCourseID, -1, self.TopLevelParent.GetStructureID(self.pageListBox.GetSelection(), 2, self.TopLevelParent.currentCourseID))
            self.TopLevelParent.DisplayDebug("Add resource to page")
        
    def MoveResourceDownOnPage(self, evt):
        if(self.usedResourcesListBox.GetSelection() != -1):
            oldResourceOrderNumber = self.usedResourcesListBox.GetSelection()
            newResourceOrderNumber = oldResourceOrderNumber + 1
            tmpString = self.usedResourcesListBox.GetStringSelection()
            tmpString2 = tmpString.split(".", 1)
            oldResourceID = int(tmpString2[0])
            tmpString = self.usedResourcesListBox.GetString((self.usedResourcesListBox.GetSelection() + 1))
            tmpString2 = tmpString.split(".", 1)
            newResourceID = int(tmpString2[0])
            con = sqlite3.connect(self.TopLevelParent.database)
            cur = con.cursor()
            cur2 = con.cursor()
            cur.execute("UPDATE ResourceTable set resourceOrderNumber = (?) where resourceID = (?)", (newResourceOrderNumber, oldResourceID))
            cur2.execute("UPDATE ResourceTable set resourceOrderNumber = (?) where resourceID = (?)", (oldResourceOrderNumber, newResourceID))
            con.commit()
            con.close()
            
            self.TopLevelParent.currentUsedResourceDict = self.TopLevelParent.RefreshListBox(self.usedResourcesListBox, self.TopLevelParent.currentCourseID, -1, self.TopLevelParent.GetStructureID(self.pageListBox.GetSelection(), 2, self.TopLevelParent.currentCourseID))
            self.TopLevelParent.DisplayDebug("move resource down on page")
        
    def MoveResourceUpOnPage(self, evt):
        if(self.usedResourcesListBox.GetSelection() != -1):
            oldResourceOrderNumber = self.usedResourcesListBox.GetSelection()
            newResourceOrderNumber = oldResourceOrderNumber - 1
            tmpString = self.usedResourcesListBox.GetStringSelection()
            tmpString2 = tmpString.split(".", 1)
            oldResourceID = int(tmpString2[0])
            tmpString = self.usedResourcesListBox.GetString((self.usedResourcesListBox.GetSelection() - 1))
            tmpString2 = tmpString.split(".", 1)
            newResourceID = int(tmpString2[0])
            con = sqlite3.connect(self.TopLevelParent.database)
            cur = con.cursor()
            cur2 = con.cursor()
            cur.execute("UPDATE ResourceTable set resourceOrderNumber = (?) where resourceID = (?)", (newResourceOrderNumber, oldResourceID))
            cur2.execute("UPDATE ResourceTable set resourceOrderNumber = (?) where resourceID = (?)", (oldResourceOrderNumber, newResourceID))
            con.commit()
            con.close()
            
            self.TopLevelParent.currentUsedResourceDict = self.TopLevelParent.RefreshListBox(self.usedResourcesListBox, self.TopLevelParent.currentCourseID, -1, self.TopLevelParent.GetStructureID(self.pageListBox.GetSelection(), 2, self.TopLevelParent.currentCourseID))
            self.TopLevelParent.DisplayDebug("Move resource up on page")
    
    def AddPage(self, evt):
        addPageDialog = wx.TextEntryDialog(self, "", "Enter the Page Title")
        if(addPageDialog.ShowModal() == wx.ID_OK):
            con = sqlite3.connect(self.TopLevelParent.database)
            cur = con.cursor()
            cur.execute("INSERT INTO StructureTable(courseID, structureTypeID, structureOrderNumber, structureName, parentID) VALUES(?, 2, ?, ?, ?)", (self.TopLevelParent.currentCourseID, self.pageListBox.GetCount(), addPageDialog.GetValue(), self.TopLevelParent.GetStructureID(self.moduleListBox.GetSelection(), 1, self.TopLevelParent.currentCourseID)))
            con.commit()
            con.close()
            self.pageListBox.Append(addPageDialog.GetValue())
        
    def DelPage(self, evt):
        con = sqlite3.connect(self.TopLevelParent.database)
        cur = con.cursor()
        cur.execute("DELETE FROM StructureTable where structureOrderNumber = (?) and courseID = (?) and structureTypeID = 2 and parentID = (?)", (self.pageListBox.GetSelection(), self.TopLevelParent.currentCourseID, self.TopLevelParent.GetStructureID(self.moduleListBox.GetSelection(), 1, self.TopLevelParent.currentCourseID)))
        con.commit()
        con.close()
        self.TopLevelParent.RefreshPageListBox(self.pageListBox, self.TopLevelParent.currentCourseID, self.TopLevelParent.GetStructureID(self.moduleListBox.GetSelection(), 1, self.TopLevelParent.currentCourseID))
        
    def PageDown(self, evt):
        curModuleID = self.TopLevelParent.GetStructureID(self.moduleListBox.GetSelection(), 1, self.TopLevelParent.currentCourseID)
        oldID = -1
        newID = -1
        con = sqlite3.connect(self.TopLevelParent.database)
        cur = con.cursor()
        cur.execute("SELECT structureID from StructureTable where structureOrderNumber = (?) and courseID = (?) and parentID = (?) and structureTypeID = 2", (self.pageListBox.GetSelection(), self.TopLevelParent.currentCourseID, curModuleID))
        for row in cur:
            oldID = row[0]
            
        con.close()
        
        con = sqlite3.connect(self.TopLevelParent.database)
        cur2 = con.cursor()
        cur2.execute("SELECT structureID from StructureTable where structureOrderNumber = (?) and courseID = (?) and parentID = (?) and structureTypeID = 2", ((self.pageListBox.GetSelection() + 1), self.TopLevelParent.currentCourseID, curModuleID))
        for row in cur2:
            newID = row[0]
            
        con.close()
        
        con = sqlite3.connect(self.TopLevelParent.database)
        cur3 = con.cursor()
        cur3.execute("UPDATE StructureTable set structureOrderNumber = (?) where structureID = (?)", ((self.pageListBox.GetSelection() + 1), oldID))
        con.commit()
        con.close()

        con = sqlite3.connect(self.TopLevelParent.database)
        cur4 = con.cursor()
        cur4.execute("UPDATE StructureTable set structureOrderNumber = (?) where structureID = (?)", (self.pageListBox.GetSelection(), newID))
        con.commit()
        con.close()

        self.TopLevelParent.RefreshPageListBox(self.pageListBox, self.TopLevelParent.currentCourseID, curModuleID)
        
    def PageUp(self, evt):
        curModuleID = self.TopLevelParent.GetStructureID(self.moduleListBox.GetSelection(), 1, self.TopLevelParent.currentCourseID)
        oldID = -1
        newID = -1
        
        con = sqlite3.connect(self.TopLevelParent.database)
        cur = con.cursor()
        cur.execute("SELECT structureID from StructureTable where structureOrderNumber = (?) and courseID = (?) and parentID = (?) and structureTypeID = 2", (self.pageListBox.GetSelection(), self.TopLevelParent.currentCourseID, curModuleID))
        for row in cur:
            oldID = row[0]
        
        con.close()

        con = sqlite3.connect(self.TopLevelParent.database)
        cur2 = con.cursor()
        cur2.execute("SELECT structureID from StructureTable where structureOrderNumber = (?) and courseID = (?) and parentID = (?) and structureTypeID = 2", ((self.pageListBox.GetSelection() - 1), self.TopLevelParent.currentCourseID, curModuleID))
        for row in cur2:
            newID = row[0]
        
        con.close()
        
        con = sqlite3.connect(self.TopLevelParent.database)
        cur3 = con.cursor()
        cur3.execute("UPDATE StructureTable set structureOrderNumber = (?) where structureID = (?)", ((self.pageListBox.GetSelection() - 1), oldID))
        con.commit()
        con.close()

        con = sqlite3.connect(self.TopLevelParent.database)
        cur4 = con.cursor()
        cur4.execute("UPDATE StructureTable set structureOrderNumber = (?) where structureID = (?)", (self.pageListBox.GetSelection(), newID))
        con.commit()
        con.close()
        
        self.TopLevelParent.RefreshPageListBox(self.pageListBox, self.TopLevelParent.currentCourseID, curModuleID)

    def AddModule(self, evt):
        addModuleDialog = wx.TextEntryDialog(self, "", "Enter the Module Title")
        if(addModuleDialog.ShowModal() == wx.ID_OK):
            con = sqlite3.connect(self.TopLevelParent.database)
            cur = con.cursor()
            cur.execute("INSERT INTO StructureTable(courseID, structureTypeID, structureOrderNumber, structureName) VALUES(?, 1, ?, ?)", (self.TopLevelParent.currentCourseID, self.moduleListBox.GetCount(), addModuleDialog.GetValue()))
            con.commit()
            con.close()
            self.moduleListBox.Append(addModuleDialog.GetValue())
        
    def DelModule(self, evt):
        con = sqlite3.connect(self.TopLevelParent.database)
        cur = con.cursor()
        cur.execute("DELETE FROM StructureTable where structureOrderNumber = (?) and courseID = (?) and structureTypeID = 1", (self.moduleListBox.GetSelection(), self.TopLevelParent.currentCourseID))
        con.commit()
        con.close()
        self.TopLevelParent.RefreshModuleListBox(self.moduleListBox, self.TopLevelParent.currentCourseID)
        
    def ModuleDown(self, evt):
        con = sqlite3.connect(self.TopLevelParent.database)
        cur = con.cursor()
        cur2 = con.cursor()
        cur3 = con.cursor()
        cur4 = con.cursor()
        cur.execute("SELECT structureID from StructureTable where structureOrderNumber = (?) and courseID = (?) and structureTypeID = 1", (self.moduleListBox.GetSelection(), self.TopLevelParent.currentCourseID))
        cur2.execute("SELECT structureID from StructureTable where structureOrderNumber = (?) and courseID = (?) and structureTypeID = 1", ((self.moduleListBox.GetSelection() + 1), self.TopLevelParent.currentCourseID))
        for row in cur:
            cur3.execute("UPDATE StructureTable set structureOrderNumber = (?) where structureID = (?)", ((self.moduleListBox.GetSelection() + 1), row[0]))
        
        for row in cur2:
            cur4.execute("UPDATE StructureTable set structureOrderNumber = (?) where structureID = (?)", (self.moduleListBox.GetSelection(), row[0]))

        con.commit()
        con.close()
        self.TopLevelParent.RefreshModuleListBox(self.moduleListBox, self.TopLevelParent.currentCourseID)
    
    def ModuleUp(self, evt):
        con = sqlite3.connect(self.TopLevelParent.database)
        cur = con.cursor()
        cur2 = con.cursor()
        cur3 = con.cursor()
        cur4 = con.cursor()
        cur.execute("SELECT structureID from StructureTable where structureOrderNumber = (?) and courseID = (?)", (self.moduleListBox.GetSelection(), self.TopLevelParent.currentCourseID))
        cur2.execute("SELECT structureID from StructureTable where structureOrderNumber = (?) and courseID = (?)", ((self.moduleListBox.GetSelection() - 1), self.TopLevelParent.currentCourseID))
        for row in cur:
            cur3.execute("UPDATE StructureTable set structureOrderNumber = (?) where structureID = (?)", ((self.moduleListBox.GetSelection() - 1), row[0]))
            
        for row in cur2:
            cur4.execute("UPDATE StructureTable set structureOrderNumber = (?) where structureID = (?)", (self.moduleListBox.GetSelection(), row[0]))
            
        con.commit()
        con.close()
        self.TopLevelParent.RefreshModuleListBox(self.moduleListBox, self.TopLevelParent.currentCourseID)
